
<script>
  import "@/assets/common/base.css";
  import leftBar from '@/components/leftBar';
  import controler from '@/components/controler';
  import list from '@/components/list';
  import topHeader from '@/components/topHeader';
  import chooseShopDailog from '@/components/chooseShopDailog';
  import editBillDailog from "@/components/editBillDailog";
  const { Fetch } = require("@/common/fetch");
  import { basicDataTest,shopDataTest,themeDataTest,dateDataTest,listDataTest } from "@/common/data.js";
  import {tools} from "@/common/utils.js";
  const _dev_ = false;
  const basicData = Fetch('/admin/finance/api/page.php?action=get_base');
  const shopData = Fetch('/admin/finance/api/page.php?action=get_shop');

  export default {
    name: 'App',
    components:{
      leftBar,controler,list,chooseShopDailog,topHeader,editBillDailog
    },
    data(){
      return {
        switchShopVisible:false,
        switchBillVisible:false,
        left_menu:_dev_ ? basicDataTest.left_menu : [],//左侧导航数据
        shops:[], //供应商店铺数据
        editData:{},
        listData:_dev_ ? listDataTest : [],
        topHeader:_dev_ ? basicDataTest.user : {},
        isEdit:false,
        currentData:{
          listData:_dev_ ? listDataTest :[],
          themeList:_dev_ ? themeDataTest : [],
          dateList:_dev_ ? dateDataTest : [],
          shop_name: '',
          shop_id:'',
          theme_id:'',
          date: tools.getParamFromUrl('date') || tools.formatYYYYMMDD(new Date(),'-')
        }

      }
    },
    methods: {
      handleChooseShop() {
        this.switchShopVisible = true;
      },
      closeChooseShop(){
        this.switchShopVisible = false;
      },
      handleEditBill({item,row}){
        this.editData = item;
        this.switchBillVisible = true;
      },
      closeBill(){
        this.switchBillVisible = false;
      },
      handleShopChange(row){
        this.currentData = {...this.currentData,...row};
        this.switchShopVisible = false;
        this.getItem(this.currentData.shop_id);
      },
      getItem(shopId){
        const url =  '/admin/finance/api/page.php?action=get_shop_theme&shop_id='+shopId,
            self =this;
        let themeData =Fetch(url);
        themeData.then((res)=>{
          if(res){
            self.currentData.themeList = res.data;
            let id = this.currentData.themeList[0] && this.currentData.themeList[0].theme_id;
            self.searchDate({
              theme_id:id,
              date:self.currentData.date,
              week:0
            })
          }
        })
      },
      searchDate({theme_id,date,week}){
        var self = this;
        let dateData = Fetch(`/admin/finance/api/theme.php?action=get_week_sum&theme_id=${theme_id}&week=${week}&date=${date}`);
        dateData.then((response)=>{
          self.currentData.dateList = response.data || [];
          let dateIndex = self.currentData.dateList.findIndex((value,index,arr)=>{
            return value.class == 'active';
          });
          self.currentData.date = self.currentData.dateList[dateIndex].date;
          self.currentData.theme_id  = theme_id;
          self.searchList({
            theme_id:theme_id,
            date:self.currentData.date
          })
        });

      },
      searchList({theme_id,date}){
        let listData = Fetch(`/admin/finance/api/theme.php?action=get_theme_info&theme_id=${theme_id}&date=${date}`);
        const self =this;
        listData.then((response)=>{
          self.currentData.listData = response.data || [];
          if(self.isEdit){
            self.currentData.listData.forEach((item)=>{
              let themeTimeId = tools.getParamFromUrl('theme_time_id');
              if(item.theme_time_id == themeTimeId){
                self.handleEditBill({item:item});
              }
            })
          }
        });
      },
      reload(){
        this.switchBillVisible = false;
        this.searchList({
          theme_id:this.currentData.theme_id,
          date:this.currentData.date
        })
      }

    },
    mounted(){
      const self = this;
      basicData.then((response)=>{
        const {left_menu,user}  = response.data;
        self.left_menu = left_menu;
        self.topHeader= user;
      });

      shopData.then((response)=>{
        const {data} = response;
        self.shops = data;
        if(data.length){
          self.currentData = {...self.currentData,...data[0]};
          self.getItem(data[0].shop_id || '');
        }
      })


    },
    updated(){
      if(!this.init){
        var edit =tools.getParamFromUrl('edit');
        console.log(edit);
        if(tools.getParamFromUrl('edit')){
          this.isEdit = true;
          this.currentData.theme_id = tools.getParamFromUrl('theme_id');
          this.currentData.date = tools.getParamFromUrl('date');
          let  week = 0,self =this;
          setTimeout(()=>{
            self.searchDate({
              theme_id:self.currentData.theme_id,
              date:self.currentData.date,
              week:week})
          },300)
        }
        this.init = true;
      }
    }

  }
</script>


<template>
  <div id="app">
    <el-container>
      <el-aside  width="170px">
        <leftBar :parcel="left_menu"/>
      </el-aside>

      <el-container>
        <el-header height="80px">
        <topHeader :parcel="topHeader"/>
        </el-header>
        <el-main class="m-container">
          <controler  @upload="handleChooseShop"  :parcel="currentData" ref="control" @searchDate="searchDate" @searchList="searchList"/>
          <list @upload="handleEditBill" :parcel="currentData"/>
        </el-main>
      </el-container>
      <chooseShopDailog :shops="shops" :currentShop="currentData.shop_id" :switchShopVisible="switchShopVisible" @closeChooseShop="closeChooseShop" @upload="handleShopChange"/>
      <editBillDailog  :switchBillVisible="switchBillVisible" @closeBill="closeBill" :parcel="editData" @upload="reload"/>
    </el-container>
  </div>
</template>



<style scoped lang = "scss">
  #app {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
    background: #F2F2F2;
    box-shadow: 0 2px 4px 0 rgba(0,0,0,0.50);
}
.el-main{padding:30px}
  .el-header{padding:0;}


</style>
